@extends('PShop::layout')
@push('styles')

@endpush
@section('content')
    <section class="wapper">
        <div class="base-w">
            <div class="">
                @component('PShop::public.crumb_nav')
                    <li><a href="#">地址管理</a></li>
                @endcomponent
            </div>
            <div class="am-g">
                @include('PShop::public.user_nav',['active'=>'address'])
                <div class="am-u-md-9 am-u-lg-10 am-padding-0">
                    @if($address->isNotEmpty())
                        <div class="am-bg-white am-padding" style="display: block;">
                        <div class="am-text-right"><span class="am-text-secondary address-create cursor-pointer"><i class="am-icon-plus am-padding-right-xs"></i>新建地址</span></div>
                        <div class="am-margin-top-sm address-manager-panel">
                            <div class="am-scrollable-horizontal">
                                <table class="am-table am-text-center am-radius">
                                    <thead>
                                    <tr>
                                        <th>收货人</th>
                                        <th>地址</th>
                                        <th>联系方式</th>
                                        <th>操作</th>
                                        <th></th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    @foreach($address as $item)
                                    <tr>
                                        <td>{{$item->consignee}}</td>
                                        <td>{{get_address($item->area).'-'.$item->address}}</td>
                                        <td>{{$item->phone}}</td>
                                        <td>
                                            <span class="am-text-secondary am-margin-right-sm cursor-pointer address-edit" data-action="{{route('f_Pc_Address_update',['addr_id'=>$item->id])}}">编辑</span>
                                            <span class="am-text-secondary cursor-pointer addr-del" data-action="{{route('f_Pc_Address_delete',['addr_id'=>$item->id])}}">删除</span>
                                        </td>
                                        <td>
                                            <label class="am-checkbox am-danger am-margin-0 am-padding-left-0 cursor-pointer">
                                                <input type="checkbox" value="{{$item->is_default??0}}" class="set-default"
                                                       data-action="{{route('f_Pc_Address_setDefault',['addr_id'=>$item->id])}}"
                                                       name="default" data-am-ucheck {{$item->is_default==1?'checked':''}} >&nbsp;&nbsp;默认地址
                                            </label>
                                        </td>
                                    </tr>
                                    @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    @else
                    <!--地址为空-->
                        <div class="am-bg-white am-padding-top-xl am-padding-bottom-xl">
                            <div class="am-text-center am-margin-xl am-padding-top-lg am-padding-bottom-lg" style="border:1px solid #EEEEEE;">
                                <img src="{{asset(config('mall.pc_assets').'/detail/img/empty.png')}}" />
                                <div class="am-link-muted am-margin-bottom-sm am-margin-top-sm">没有可用的地址</div>
                                <div class="address-edit am-btn am-btn-danger am-radius address-create">添加地址</div>
                            </div>
                        </div>
                    @endif

                </div>
            </div>
        </div>
    </section>
@endsection
@push('scripts')
<script>
    $('.address-create').click(function(){
        layer.open({
            type: 2,
            title:"添加收货地址",
            area: ['700px', '400px'], //宽高
            content: '{{route('f_Pc_Address_create')}}'
        });
    });
    $('.address-edit').click(function(){
        var url = $(this).data('action')
        layer.open({
            type: 2,
            title:"编辑收货地址",
            area: ['700px', '400px'], //宽高
            content: url
        });
    });
    $('.addr-del').each(function () {
        $(this).off('click').on('click',function () {
            var url = $(this).data('action');
            layer.confirm('确定要删除该地址吗?',function () {
                $.get(url,function (response) {
                    if(response.status == 1){
                        layer.msg('删除成功');
                        window.location.reload();
                        return true;
                    }else{
                        layer.msg(response.msg);
                        return true;
                    }
                });
            });

        });
    });
    $('.set-default').each(function () {
        $(this).off('click').on('click',function () {
            if($(this).val() == 1){
                this.checked = true;
                return false;
            }
            var url = $(this).data('action');
            layer.confirm('确定要设为默认地址吗?',function () {
                $.get(url,function (response) {
                    if(response.status == 1){
                        layer.msg('设置成功');
                        window.location.reload();
                        return true;
                    }else{
                        layer.msg(response.msg);
                        return true;
                    }
                });
            });
            this.checked = false;
        });
    })
</script>
@endpush